﻿/*-表单窗体公共函数-*/
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) {
        return window.CloseOwnerWindow(action);
    }
    else {
        window.close();
    }
}
function onCancel(e) {
    CloseWindow("cancel");
}
function onOk(e) {
    CloseWindow("ok");
}

function indexOf(item, array, method) {
    for (var i = 0; i < array.length; i++) {
        if (method(item, array[i])) {
            return i;
        }
    }
    return -1;
}

function exists(item, array, comparer) {
    for (var i = 0; i < array.length; i++) {
        if (comparer(item, array[i])) { return true; }
    }
    return false;
}

if (!Date.prototype.toISOString) {
    Date.prototype.toISOString = function () {
        function pad(n) { return n < 10 ? '0' + n : n }
        return this.getUTCFullYear() + '-'
    + pad(this.getUTCMonth() + 1) + '-'
    + pad(this.getUTCDate()) + 'T'
    + pad(this.getUTCHours()) + ':'
    + pad(this.getUTCMinutes()) + ':'
    + pad(this.getUTCSeconds()) + '.'
    + pad(this.getUTCMilliseconds()) + 'Z';
    }
}

function mask(html) {
    if (!html) { html = "please waiting..."; }
    mini.mask({ el: document.body, cls: 'mini-mask-loading', html: html });
}
function unmask(el) {
    if (!el) {
        el = document.body;
    }
    mini.unmask(el);
}
function htmlDecode(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
}

$.extend({
    /** 
    1. 设置cookie的值，把name变量的值设为value   
    example $.cookie(’name’, ‘value’);
    2.新建一个cookie 包括有效期 路径 域名等
    example $.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});
    3.新建cookie
    example $.cookie(’name’, ‘value’);
    4.删除一个cookie
    example $.cookie(’name’, null);
    5.取一个cookie(name)值给myvar
    var account= $.cookie('name');
    **/
    cookie: function (name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            var path = options.path ? '; path=' + options.path : '';
            var domain = options.domain ? '; domain=' + options.domain : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    },
    confirm: function (text, callback) {
        mini.showMessageBox({
            title: "确认删除",
            message: text,
            buttons: ["是", "否"],
            iconCls: "mini-messagebox-question",
            callback: callback
        });
    },

    alert: function (text, title, iconCls) {
        if (title == null || typeof (title) == "undefined") {
            title = "提醒";
        }
        if (iconCls == null || typeof (iconCls) == "undefined") {
            iconCls = "mini-messagebox-warning";
        }
        mini.showMessageBox({
            title: title,
            message: text,
            buttons: ["确定"],
            iconCls: iconCls
        });
    },
    error: function (text) {
        mini.showMessageBox({
            title: "错误",
            message: text,
            buttons: ["确定"],
            iconCls: "mini-messagebox-error"
        });
    },
    notify: function (message, title, iconCls, timeout, x, y, width) {
        if (!message) { return; }
        if (!title) { title =  "提示"; }
        if (!iconCls) { iconCls = "mini-messagebox-info"; }
        if (!timeout) { timeout = 2000; }
        if (!width) { width = 250; }
        if (!x) { x = "center"; }
        if (!y) { y = "center"; }
        var a = mini.showMessageBox({
            showModal: false,
            width: width,
            title: title,
            iconCls: iconCls,
            message: message,
            timeout: timeout,
            x: x,
            y: y
        });
        setTimeout(function () { mini.hideMessageBox(a); }, timeout);
    }
});

function htmlEncode(s) {
    var div = document.createElement('div');
    div.appendChild(document.createTextNode(s));
    return div.innerHTML;
}

function htmlDecode(s) {
    var div = document.createElement('div');
    div.innerHTML = s;
    return div.innerText || div.textContent;
}
function isTrue(obj) {
    if (typeof (obj) == "undefined") { return false; }
    if (obj == null) { return false; }
    if (obj == false) { return false; }
    return true;
}